package com.blb.thread;

/**
 * 合并线程，让其它线程先执行
 */
public class JoinThreadDemo {

    static Thread thread1;
    static Thread thread2;

    public static void main(String[] args) {
        thread1 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(i == 50){
                    try {
                        thread2.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName() + "-->" + i);
            }
        });
        thread2 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //合并thread1
                if(i == 50){
                    try {
                        thread1.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName() + "-->" + i);
            }
        });
        thread1.setName("张三");
        thread2.setName("李四");
        thread1.start();
        thread2.start();
    }
}
